<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :error.sync="error"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from "@/api/search";
export default {
  name: "SearchResult",
  components: {},
  props: {
    searchText: {
      type: String,
      required: true,
     
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 20,
      error: false
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onLoad() {
      try {
        const { data } = await getSearchResult({
          page: this.page, //页码
          per_page: this.per_page, //每页大小
          q: this.searchText
        });
        const { results } = data.data;
        this.list.push(...results);
        // 将本次加载中的 loading 关闭
        this.loading = false;
        if (results.length) {
          // 如果有，更新获取下一个数据的页码
          this.page ++;
        } else {
          // 如果没有 将加载状态 finished 设置为结束
          this.finished = true;
        }
      } catch (err) {
        // 展示失败的提示状态
        this.error = true;
        // 加载失败了 loading 也要关闭
        this.loading = false;
        console.log(err);
      }
    }
  }
};
</script>

<style scoped lang="less"></style>
